<template>
  <div>
    <el-button type="primary" @click="add">添加</el-button>
    <list-vue  :info="info" :arr="arr" @init="getList" @willUpdate="willUpdate"></list-vue>
    <form-vue :info="info" :arr="arr" @init="getList" ref="form"></form-vue>
    <el-pagination
      @current-change="changePage"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="size"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqrolelist, requserlist ,requsercount} from "../../http/api";
import formVue from './components/form.vue'
import listVue from './components/list.vue'
export default {
  components:{
    formVue,
    listVue
  },
  data() {
    return {
      info:{
        isshow:false,
        isAdd:false
      },
      arr:[],
      total: 0, //总条目数
      size: 2, //一页的数量
      page: 1, //当前页码
    }
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {
    this.getList()
    // requserlist()
  },
  methods: {
    ...mapActions({}),
    willUpdate(id){
      this.isshow = true;
      this.isAdd = false;
      this.$refs.form.getOne(id)
    },
    getList(){
      // 请求列表
        this.requestlist();
        //请求总数
        requsercount().then(res => {
          this.total = res.data.list[0].total;
        })
    },
    requestlist(){
      requserlist({size: this.size,page: this.page}).then(res => {
          let arr = res.data.list ? res.data.list : [];
          if(this.page != 1 && arr.length == 0){
            this.page--;
            this.requestlist();
            return 
          }
          this.arr = arr;
      })
    },
    add(){
      this.info.isshow = true;
      this.info.isAdd = true;
    },
    changePage(e){
      // console.log(e);
      this.page = e;
      this.requestlist();
    }
  },
};
</script>
<style scoped lang="less">
@import "../../less/index.less";
.el-button{
  margin: 20px;
}
</style>